/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.foridev.gec.gui;

import com.foridev.gec.dao.ActeDao;
import com.foridev.gec.model.Acte;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.util.Callback;

/**
 * FXML Controller class
 *
 * @author amine
 */
public class NaissanceRecordController implements Initializable {

    @FXML
    TableView<Acte> tv;
    @FXML
    TableColumn<Acte, Integer> tcNumero;
    @FXML
    TableColumn<Acte, String> tcNumActe;
    @FXML
    TableColumn<Acte, Integer> tcAnneeActe;
    @FXML
    TableColumn<Acte, String> tcNomFr;
    @FXML
    TableColumn<Acte, String> tcPrenomFr;
    @FXML
    TableColumn<Acte, Date> tcDateNaiss;
    @FXML
    TableColumn<Acte, String> tcLieuNaissFr;
    @FXML
    TableColumn<Acte, String> tcDeces;
    @FXML
    TableColumn<Acte, String> tcNomAr;
    @FXML
    TableColumn<Acte, String> tcPrenomAr;
    @FXML
    TableColumn<Acte, String> tcLieuNaissAr;
    @FXML
    TextField txtNumActe;
    @FXML
    TextField txtAnneeActe;
    @FXML
    TextField txtNomFr;
    @FXML
    TextField txtPrenomFr;
    @FXML
    TextField txtNomAr;
    @FXML
    TextField txtPrenomAr;
    @FXML
    TextField txtDateNaissFr;
    @FXML
    TextField txtDateNaissAr;
    @FXML
    TextField txtLieuNaissFr;
    @FXML
    TextField txtLieuNaissAr;
    @FXML
    TextField txtNomPereFr;
    @FXML
    TextField txtNomPereAr;
    @FXML
    TextField txtNomMereFr;
    @FXML
    TextField txtNomMereAr;
    @FXML
    TextField txtdeces;
    @FXML
    ComboBox<Integer> cmbDeces;
    @FXML
    RadioButton rbMasculin;
    @FXML
    RadioButton rbFeminin;
    @FXML
    AnchorPane pnForm;
    @FXML
    AnchorPane pnForm2;
    @FXML
    AnchorPane grid;
    ActeDao acteDao;
    Acte acte;
    ObservableList<Acte> obl = FXCollections.emptyObservableList();
    Pagination pagination;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {

        tcNumero.setCellValueFactory(new PropertyValueFactory<Acte, Integer>("id"));
        tcNumActe.setCellValueFactory(new PropertyValueFactory<Acte, String>("numActeFr"));
        tcAnneeActe.setCellValueFactory(new PropertyValueFactory<Acte, Integer>("dateActe"));
        tcNomFr.setCellValueFactory(new PropertyValueFactory<Acte, String>("nomFr"));
        tcPrenomFr.setCellValueFactory(new PropertyValueFactory<Acte, String>("prenomFr"));
        tcDateNaiss.setCellValueFactory(new PropertyValueFactory<Acte, Date>("neLe"));
        tcLieuNaissFr.setCellValueFactory(new PropertyValueFactory<Acte, String>("lieuNaissFr"));
        tcDeces.setCellValueFactory(new PropertyValueFactory<Acte, String>("indicDeceFr"));
        tcNomAr.setCellValueFactory(new PropertyValueFactory<Acte, String>("nomAr"));
        tcPrenomAr.setCellValueFactory(new PropertyValueFactory<Acte, String>("prenomAr"));
        tcLieuNaissAr.setCellValueFactory(new PropertyValueFactory<Acte, String>("lieuNaissAr"));

        tv.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Acte>() {
            @Override
            public void changed(ObservableValue<? extends Acte> observable, Acte oldValue, Acte newValue) {
                editRecord(newValue);
            }
        });

        //listRecords();
        acteDao = new ActeDao();
        final int total = acteDao.getTotalCount();
        System.out.println("*************************  TOTAL = " + total);
        acteDao.close();
        int pages = total / rowsPerPage();
        pagination = new Pagination(pages % 2 == 0 ? pages : pages + 1, 0);
        //pagination.setStyle("-fx-border-color:red;");
        pagination.setPageFactory(new Callback<Integer, Node>() {
            @Override
            public Node call(Integer pageIndex) {
                if (pageIndex > total / rowsPerPage() + 1) {
                    return null;
                } else {
                    return createPage(pageIndex);
                }
            }
        });
        //pagination.
        grid.getChildren().clear();
        grid.getChildren().addAll(pagination);

    }

    @FXML
    protected void processNouveau() {
        newRecord();
    }

    @FXML
    protected void processEnregistrer() {
        saveRecord();
    }

    public void listRecords() {
        acteDao = new ActeDao();
        obl = FXCollections.observableArrayList(acteDao.getActesWithFilters());
        tv.setItems(obl);
        acteDao.close();
    }

    public void newRecord() {
        acte = new Acte();
        pnForm.setDisable(false);
        clearField();
    }

    public void editRecord(Acte a) {

        if (a != null) {
            pnForm.setDisable(false);
            acteDao = new ActeDao();

            acte = acteDao.getById(a.getId());

            txtNumActe.setText(acte.getNumActeFr());
            txtAnneeActe.setText(acte.getDateActe() != null ? acte.getDateActe().toString() : "");
            txtNomFr.setText(acte.getNomFr());
            txtNomAr.setText(acte.getNomAr());
            txtPrenomFr.setText(acte.getPrenomFr());
            txtPrenomAr.setText(acte.getPrenomAr());
            txtDateNaissFr.setText(acte.getAnneeNaissFr());
            txtDateNaissAr.setText(acte.getAnneeNaissAr());
            txtLieuNaissFr.setText(acte.getLieuNaissFr());
            txtLieuNaissAr.setText(acte.getLieuNaissAr());
            txtNomPereFr.setText(acte.getNomPereFr());
            txtNomPereAr.setText(acte.getNomPereAr());
            txtNomMereFr.setText(acte.getNomMereFr());
            txtNomMereAr.setText(acte.getNomMereAr());

            if (acte.getSexe() != null) {
                if (acte.getSexe() == 0) {
                    rbMasculin.setSelected(true);
                    rbFeminin.setSelected(false);
                } else {
                    rbFeminin.setSelected(true);
                    rbMasculin.setSelected(false);
                }
            }

            acteDao.close();
            //cmbDeces.setValue(0);
        }
    }

    public void saveRecord() {
        acteDao = new ActeDao();
        acte.setNumActeFr(txtNumActe.getText());
        acte.setDateActe(Integer.valueOf(txtAnneeActe.getText()));
        acte.setNomFr(txtNomFr.getText());
        acte.setNomAr(txtNomAr.getText());
        acte.setPrenomFr(txtPrenomFr.getText());
        acte.setPrenomAr(txtPrenomAr.getText());
        if (rbMasculin.isSelected()) {
            acte.setSexe(0);
        } else {
            acte.setSexe(1);
        }
        acte.setAnneeNaissFr(txtDateNaissFr.getText());
        acte.setAnneeNaissAr(txtDateNaissAr.getText());
        acte.setLieuNaissFr(txtLieuNaissFr.getText());
        acte.setLieuNaissAr(txtLieuNaissAr.getText());
        acte.setNomPereFr(txtNomPereFr.getText());
        acte.setNomPereAr(txtNomPereAr.getText());
        acte.setNomMereFr(txtNomMereFr.getText());
        acte.setNomMereAr(txtNomMereAr.getText());

        acteDao.persist(acte);
        acteDao.close();
        pnForm.setDisable(true);
        clearField();
        listRecords();
    }

    public void clearField() {
        txtNumActe.setText("");
        txtAnneeActe.setText("");
        txtNomFr.setText("");
        txtNomAr.setText("");
        txtPrenomFr.setText("");
        txtPrenomAr.setText("");
        rbFeminin.setSelected(false);
        rbMasculin.setSelected(true);
        txtDateNaissFr.setText("");
        txtDateNaissAr.setText("");
        txtLieuNaissFr.setText("");
        txtLieuNaissAr.setText("");
        txtNomPereFr.setText("");
        txtNomPereAr.setText("");
        txtNomMereFr.setText("");
        txtNomMereAr.setText("");
        cmbDeces.setValue(0);
    }

    public int itemsPerPage() {
        return 1;
    }

    public int rowsPerPage() {
        return 2;
    }

    public VBox createPage(int pageIndex) {
        System.out.println("createPage = " + pageIndex);

        VBox box = new VBox(5);
        int page = pageIndex * itemsPerPage();

        for (int i = page; i < page + itemsPerPage(); i++) {
            TableView<Acte> table = tv;
            acteDao = new ActeDao();
            table.setItems(FXCollections.observableArrayList(acteDao.getAll(0, "nomFr", pageIndex * rowsPerPage(), rowsPerPage())));
            box.getChildren().add(table);
            acteDao.close();
        }
        return box;
    }
}
